﻿using System.Linq;
using Domain.Setup.Tasks;
using System;

namespace Domain.Setup.Skills
{
    /// <summary>
    /// The skills domain service coordinates entities in the System Setup Domain for the purpose of achieving some business
    /// function.
    /// </summary>
    public class SkillsDomainService : ISetupSkillDomainService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SkillsDomainService"/> class.
        /// </summary>
        /// <param name="taskRepository">The task repository.</param>
        /// <param name="skillRepository">The skill repository.</param>
        public SkillsDomainService(ITaskRepository taskRepository, ISkillRepository skillRepository)
        {
            // Setup local references to dependencies.
            if (taskRepository == null)
            {
                throw new ArgumentException("A valid ITaskRepository instance is required.");          
            }
            if (skillRepository == null)
            {
                throw new ArgumentException("A valid ISkillRepository instance is required.");
            }

            this.TaskRepository = taskRepository;
            this.SkillRepository = skillRepository;
        }

        #region Public Properties
        public ISkillRepository SkillRepository { get; private set; }
        public ITaskRepository TaskRepository { get; private set; } 
        #endregion
        
        /// <summary>
        /// Deletes the skill.
        /// </summary>
        /// <param name="skill">The skill.</param>
        public void DeleteSkill(Entities.Skill skill)
        {
            // Default in use flag to false
            bool inUse = false;

            // Find any Dependant Tasks.
            var dependantTasks = TaskRepository.Get(x => x.RequiredSkill.Id == skill.Id);

            // If there are dependent tasks set the in use flag to true and continue
            if (dependantTasks.Count() > 0)
            {
                inUse = true;
            }
            else // else continue checking the other repostories.
            {
                #warning Will need to check in other repositories if this Skill was used in any Treatment/Schedule/Study, Update as inactive if already referenced.
            }

            // If in use, flag as inactive
            if (inUse)
            {
                skill.SetInactive();
            }
            else // else go ahead and delete from the database
            {
                SkillRepository.Delete(skill);
            }
        }

        /// <summary>
        /// Adds a task that requires the specified Skill to the domain.
        /// </summary>
        /// <param name="task">The task.</param>
        /// <param name="skillId">The skill id.</param>
        /// <returns></returns>
        public Entities.Task AddTask(Entities.Task task, int skillId)
        {
            Entities.Task response = null;

            // Find the required Skill in storage
            var skill = SkillRepository.Get(x => x.Id == skillId);
            
            // If the skill is found go ahead and add the task
            if (skill.Count() > 0)
            {
                task.RequiredSkill = skill.First();
                response = TaskRepository.Add(task);
            }

            return response;
        }
    }
}
